/**
 * 
 */
package data.mysql;

import java.util.HashMap;
import java.util.List;

import bean.Component;
import data.IComponentDao;
import data.Pagination;
import data.exception.DaoException;

/**
 * Component DAO.
 * 
 * @author chuxiaoyuan
 *
 */
public class ComponentDao extends BaseDaoSupport<Component> implements IComponentDao {

	@Override
	protected Component createBean() {

		return new Component();
	}

	/* (non-Javadoc)
	 * @see data.IComponentDao#updateComponentInfo(bean.Component)
	 */
	@Override
	public int updateComponentInfo(Component component) throws DaoException {

		// Build SQL
		String preparedSQL = "UPDATE " + this.getTableName()
				+ " SET name=?, price=?, description=? WHERE id=?";

		// Execute update
		return this.executeUpdate(preparedSQL, 
				component.getName(), component.getPrice(), 
				component.getDescription(), component.getId());
	}
	
	/* (non-Javadoc)
	 * @see data.IComponentDao#increaseComponentQuantity(java.lang.Long, java.lang.Long)
	 */
	@Override
	public int increaseComponentQuantity(Long componentId, Long number)
			throws DaoException {
		// Build SQL
		String preparedSQL = "UPDATE " + this.getTableName()
				+ " SET quantity=quantity+? WHERE id=?";

		// Execute update
		return this.executeUpdate(preparedSQL, number, componentId);
	}

	/* (non-Javadoc)
	 * @see data.IComponentDao#decreaseComponentQuantity(java.lang.Long, java.lang.Long)
	 */
	@Override
	public int decreaseComponentQuantity(Long componentId, Long number)
			throws DaoException {

		// Build SQL
		String preparedSQL = "UPDATE " + this.getTableName()
				+ " SET quantity=quantity-? WHERE id=?";

		// Execute update
		return this.executeUpdate(preparedSQL, number, componentId);
	}

	/* (non-Javadoc)
	 * @see data.IComponentDao#selectById(java.lang.Long)
	 */
	@SuppressWarnings("serial")
	@Override
	public Component selectById(final Long componentId) throws DaoException {

		// Select by primary key.
		return this.selectByPrimaryKey(new HashMap<String, Object>() {
			{
				put("id", componentId);
			}
		});
	}

	/* (non-Javadoc)
	 * @see data.IComponentDao#selectByKeywords(java.lang.String, data.Pagination)
	 */
	@Override
	public List<Component> selectByKeywords(String keyword,
			Pagination pagination) throws DaoException {

		// Build SQL: matching keyword in name or description.
		// Ignore cases: all to upper case.
		String preparedSQL = this.buildSelectClause(this.getFields(),
				this.getTableName());
		preparedSQL += " WHERE UPPER(name) LIKE ? OR UPPER(description) LIKE ?";

		// Execute query
		keyword = "%" + keyword.toUpperCase() + "%";
		List<Component> results = this.executeQuery(this, this.getFields(),
				preparedSQL, keyword, keyword);

		// Return bean
		return results;
	}

	/* (non-Javadoc)
	 * @see data.IComponentDao#selectByKeywords(java.lang.String)
	 */
	@Override
	public List<Component> selectByKeywords(String keyword)
			throws DaoException {
		// Without pagination
		return this.selectByKeywords(keyword, null);
	}



}
